var WPAPI = require( 'wpapi' );
const axiosReq = require('hy5b/axiosReq/axiosReq')

const baseUrl = 'http://your-domain/wordpress';

const request = axiosReq.create({
  baseURL: baseUrl + '/index.php/wp-json'
})

// Authorization的生成, 看插件JWT Authentication for WP-API的说明
request.interceptors.request.use((config) => {
  config.headers = {
    Authorization: 'Bearer xxxxxxxxxxxxxxxxxxxx',
  };
  return config;
}, (err) => {
  console.error(333, err);
})

/**
 * @param {https://developer.wordpress.org/rest-api/reference/posts/#arguments-2} post 
 * @returns 
 */
function post2Wordpress(post) {
  return request.post('/wp/v2/posts', post || {
    // "title" and "content" are the only required properties
    title: 'Your Post Title',
    content: 'Your post content',
    // Post will be created as a draft by default if a specific "status"
    // is not specified
    status: 'publish'
  }).then(function( response ) {
    // "response" will hold all properties of your newly-created post,
    // including the unique `id` the post was assigned on creation
    console.log(11111110,  response.id );
  
    return response
  }).catch(err => {
    console.error(1111111, err)
  })
}

// 使用sdk是需要安装相应的basic auth插件的,但是我没有成功过,因为之前乱修改.htaccess文件,导致discover到的endpoint无法请求,所以安装了插件也没效果
// 后来一番折腾偶然弄清.htaccess是由固定链接配置后自动产生的, 却再也没有尝试使用basic auth了
// 后来又发现只有使用年/月/日这种固定链接时,才可以调用api,但是页面文章都打不开了(本来好好的,重启了一下就再也不行了),而使用id时,页面文章可以打开,api又调不到
// 后来发现固定链接要自定义加上/index.php/一节(默认是这样配置的,只是我后来给换掉了,因为不支持中文标题,通过安装插件解决), 可以兼顾api和打开页面文章,似乎兜了一圈又回来了
// 但是后面发现api只能获取token,无法post文章,修复固定链接也没用,形为十分诡异令人琢磨不透,一气之下全删了,还是用回ghost吧(用pm2启动)
const config = {
  username: 'username',
  password: 'password'
}

function getSite(cb) {
  return WPAPI.discover(baseUrl).then(function (site) {
    return site.auth({ ...config });
  }).then(function (site) {
    // If default routes were detected, they are now available
    cb(site)
  })
}

module.exports = function (post) {
  switch (1) {
    case 1:
      post2Wordpress(post)
      break;
    case 2: //获取url前缀
      getSite(function (site) {
        console.log(22222222222, site)
        return;
        // If default routes were detected, they are now available
        // site.posts().then(function( posts ) {
          // console.log( posts );
        // }); // etc

        // return

        /* 下面的是可以的，不过此分支的本意是获取endpoint信息 */
        site.posts().create({
          // "title" and "content" are the only required properties
          title: 'Your Post Title',
          content: 'Your post content',
          // Post will be created as a draft by default if a specific "status"
          // is not specified
          status: 'publish'
        }).then(function(response) {
          // "response" will hold all properties of your newly-created post,
          // including the unique `id` the post was assigned on creation
          console.log(2222220, response.id);
        }).catch(function(err) {
          console.log(2222221, err)
        })
      })
      break;
    case 3: //使用sdk post文章,需要先认证
      // You must authenticate to be able to POST (create) a post
      var wp = new WPAPI({
        endpoint: baseUrl + '/index.php/wp-json',
        // This assumes you are using basic auth, as described further below
        ...config,
        auth: true
      });
      wp.posts().create({
        // "title" and "content" are the only required properties
        title: 'Your Post Title',
        content: 'Your post content',
        // Post will be created as a draft by default if a specific "status"
        // is not specified
        status: 'publish'
      }).then(function(response) {
        // "response" will hold all properties of your newly-created post,
        // including the unique `id` the post was assigned on creation
        console.log(33333330, response.id);
      }).catch(function(err) {
        console.log(33333331, err)
      })
      break;
    default:
      break;
  }
}

